using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Text.RegularExpressions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;


namespace PointBridge.SPDX
{
    public class Initialization
    {
        public static void ResetControl(RadioButtonList wRadioButtonList)
        {
            wRadioButtonList.Attributes["style"] = "";
        }
        public static void ResetControl(CheckBox wCheckBox)
        {
            wCheckBox.Attributes["style"] = "";
        }
        public static void ResetControl(TextBox wTextBox)
        {
            wTextBox.Attributes["style"] = "";
        }
        public static void ResetControl(DropDownList wDropDownList)
        {
            wDropDownList.Attributes["style"] = "";
        }
        public static void ResetControl(HiddenField wHiddenField)
        {
            //do nothing;
        }
        public static void ResetControl(ListBox wListBox)
        {
            wListBox.Attributes["style"] = "";
        }        
        
        public static void InitCheckBox(CheckBox wCheckBox, bool val)
        {
            wCheckBox.Checked = val;
            wCheckBox.TabIndex = 10;
        }
        public static void InitCheckBox(CheckBox wCheckBox, object val)
        {
            if (val == null) wCheckBox.Checked = false;
            else wCheckBox.Checked = (bool)val;
            wCheckBox.TabIndex = 10;
        }
        public static void InitSPDateTimeControl(DateTimeControl wDateTimeControl, object dateTime)
        {
            wDateTimeControl.ErrorMessage = "Invalid date";
            if (dateTime == null) wDateTimeControl.ClearSelection();
            else wDateTimeControl.SelectedDate = (DateTime)dateTime;
        }
        public static void InitDropDown(DropDownList wDropDown, int val, string label)
        {
            List<int> values = new List<int>();
            List<string> labels = new List<string>();
            labels.Add(label);
            values.Add(val);
            Initialization.InitDropDown(wDropDown, values, labels, values[0]);
        }
        public static void InitDropDown(DropDownList wDropDown, List<string> values, string selectedVal)
        {
            bool itemSelected = false;
            wDropDown.Items.Clear();
            for (int i = 0; i < values.Count; i++)
            {
                string val = values[i];
                ListItem item = new ListItem(val, val);
                if (val == selectedVal && itemSelected == false)
                {
                    item.Selected = true;
                    itemSelected = true;
                }
                wDropDown.Items.Add(item);
            }
            wDropDown.TabIndex = 10;
        }
        public static void InitDropDown(DropDownList wDropDown, List<int> values, List<string> labels, int selectedVal)
        {
            wDropDown.Items.Clear();
            for (int i = 0; i < values.Count; i++)
            {
                string label = labels[i];
                int val = values[i];
                ListItem item = new ListItem(label, val.ToString());
                if (val == selectedVal) item.Selected = true;
                wDropDown.Items.Add(item);
            }
            wDropDown.TabIndex = 10;
        }
        public static void InitDropDownFromSPChoiceField(DropDownList wDropDown, SPWeb web, string listName, string fieldName, object selectedChoice)
        {
            List<string> values = new List<string>();
            foreach (string choice in ((SPFieldChoice)web.Lists[listName].Fields[fieldName]).Choices) values.Add(choice);
            if (selectedChoice == null) selectedChoice = values[0];
            Initialization.InitDropDown(wDropDown, values, (string)selectedChoice);
        }
        public static void InitDropDownFromSPListSortAphabetically(DropDownList wDropDown, SPWeb web, string lookupListName, string lookupFieldName, object selectedLabel, bool addSelectedLabelIfDoesNotExist)
        {
            string strSelectedLabel;

            if (selectedLabel != null)
            {
                if (selectedLabel.GetType() == typeof(string)) strSelectedLabel = (string)selectedLabel;
                else if (selectedLabel.GetType() == typeof(DateTime)) strSelectedLabel = ((DateTime)selectedLabel).ToShortDateString();
                else throw new Exception("InitDropDownFromSPList: Unsupported type ('" + selectedLabel.GetType().ToString() + "')");
            }
            else
            {
                strSelectedLabel = null;
            }
            SortedDictionary<string, int> dict = new SortedDictionary<string, int>();
            int? selectedId = null;
            foreach (SPListItem item in web.Lists[lookupListName].Items)
            {
                object label = item[lookupFieldName];
                if (item[lookupFieldName].GetType() == typeof(string))
                {
                    dict.Add((string)label, item.ID);
                    if (strSelectedLabel != null) if (strSelectedLabel.Trim() == ((string)label).Trim()) selectedId = item.ID;
                }
                else if (item[lookupFieldName].GetType() == typeof(DateTime))
                {
                    dict.Add(((DateTime)label).ToShortDateString(), item.ID);
                    if (strSelectedLabel != null) if (strSelectedLabel == ((DateTime)label).ToShortDateString()) selectedId = item.ID;
                }
                else
                {
                    throw new Exception("InitDropDownFromSPList: Unsupported type ('" + selectedLabel.GetType().ToString() + "')");
                }
            }

            List<int> values = new List<int>();
            List<string> labels = new List<string>();
            values.Add(0);
            labels.Add("[None]");
            foreach (KeyValuePair<string, int> pair in dict)
            {
                values.Add(pair.Value);
                labels.Add(pair.Key);
            }

            if (labels.Count == 0) throw new Exception("InitDropDownFromSPList: Lookup list ('" + lookupListName + "') is empty");
            if (selectedLabel == null) selectedId = values[0];
            if (selectedId == null)
            {
                if (addSelectedLabelIfDoesNotExist == true)
                {
                    labels.Add(strSelectedLabel);
                    values.Add(-1);
                }
                else
                {
                    throw new Exception("Selected label ('" + selectedLabel + "') deos not exist in options");
                }
            }
            Initialization.InitDropDown(wDropDown, values, labels, (int)selectedId);
        }

        public static void InitDropDownFromSPList(DropDownList wDropDown, SPWeb web, string lookupListName, string lookupFieldName, object selectedLabel, bool addSelectedLabelIfDoesNotExist)
        {
            string strSelectedLabel;

            if (selectedLabel != null)
            {
                if (selectedLabel.GetType() == typeof(string)) strSelectedLabel = (string)selectedLabel;
                else if (selectedLabel.GetType() == typeof(DateTime)) strSelectedLabel = ((DateTime)selectedLabel).ToShortDateString();
                else throw new Exception("InitDropDownFromSPList: Unsupported type ('" + selectedLabel.GetType().ToString() + "')");
            }
            else
            {
                strSelectedLabel = null;
            }

            List<int> values = new List<int>();
            List<string> labels = new List<string>();
            int? selectedId = null;
            values.Add(0);
            labels.Add("[None]");
            foreach (SPListItem item in web.Lists[lookupListName].Items)
            {
                object label = item[lookupFieldName];
                values.Add(item.ID);
                if (item[lookupFieldName].GetType() == typeof(string))
                {
                    labels.Add((string)label);
                    if (strSelectedLabel != null) if (strSelectedLabel.Trim() == ((string)label).Trim()) selectedId = item.ID;
                }
                else if (item[lookupFieldName].GetType() == typeof(DateTime))
                {
                    labels.Add(((DateTime)label).ToShortDateString());
                    if (strSelectedLabel != null) if (strSelectedLabel == ((DateTime)label).ToShortDateString()) selectedId = item.ID;
                }
                else
                {
                    throw new Exception("InitDropDownFromSPList: Unsupported type ('" + selectedLabel.GetType().ToString() + "')");
                }
            }
            if (labels.Count == 0) throw new Exception("InitDropDownFromSPList: Lookup list ('" + lookupListName + "') is empty");
            if (selectedLabel == null) selectedId = values[0];
            if (selectedId == null)
            {
                if (addSelectedLabelIfDoesNotExist == true)
                {
                    labels.Add(strSelectedLabel);
                    values.Add(-1);
                }
                else
                {
                    throw new Exception("Selected label ('" + selectedLabel + "') deos not exist in options");
                }
            }
            Initialization.InitDropDown(wDropDown, values, labels, (int)selectedId);
        }
        public static void InitTextBoxNegative(TextBox wTextBox, float val)
        {
            wTextBox.Text = Common.PrintF2Negative(val);
            wTextBox.TabIndex = 10;
        }
        public static void InitTextBox(TextBox wTextBox, int val)
        {
            wTextBox.Text = Common.PrintF0(val);
            wTextBox.TabIndex = 10;
        }
        public static void InitTextBoxF0(TextBox wTextBox, float val)
        {
            wTextBox.Text = Common.PrintF0(val);
            wTextBox.TabIndex = 10;
        }
        public static void InitTextBoxF2(TextBox wTextBox, float val)
        {
            wTextBox.Text = Common.PrintF2(val);
            wTextBox.TabIndex = 10;
        }
        public static void InitTextBox(TextBox wTextBox, DateTime val)
        {
            wTextBox.Text = Common.Print(val);
            wTextBox.TabIndex = 10;
        }
        public static void InitTextBoxMMYYYY(TextBox wTextBox, DateTime val)
        {
            wTextBox.Text = Common.PrintMMYYYY(val);
            wTextBox.TabIndex = 10;
        }
        public static void InitTextBox(TextBox wTextBox, string val)
        {
            wTextBox.Text = val;
            wTextBox.TabIndex = 10;
        }
        public static void InitTextBox(TextBox wTextBox, object val)
        {
            if (val == null) wTextBox.Text = "";
            else wTextBox.Text = val.ToString();
        }
        public static void InitPwdTextBox(TextBox wTextBox, string val)
        {
            //doing it twice to full password prerender method
            wTextBox.Text = val;
            wTextBox.Attributes["value"] = val;
            wTextBox.TabIndex = 10;
        }
        public static void InitHiddenField(HiddenField wHiddenField, string val)
        {
            wHiddenField.Value = val;
        }
        public static void InitLabel(Label wLabel, object val)
        {
            if (val == null) wLabel.Text = "";
            else wLabel.Text = val.ToString();
        }
        public static void InitLabel(Label wLabel, string val)
        {
            wLabel.Text = val;
        }
        public static void InitLabel(Label wLabel, int val)
        {
            wLabel.Text = Common.PrintF0(val);
        }
        public static void InitLabelF2(Label wLabel, float val)
        {
            wLabel.Text = Common.PrintF2(val);
        }
        public static void InitLabelF0(Label wLabel, float val)
        {
            wLabel.Text = Common.PrintF0(val);
        }
    }
}

